package com.eleadin.ruleengine.controller;

import com.eleadin.ruleengine.controller.base.BaseController;
import com.eleadin.ruleengine.service.UserService;
import com.eleadin.ruleengine.service.vo.user.*;
import com.eleadin.ruleengine.token.NoneAuth;
import com.eleadin.ruleengine.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @ClassName UserController
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @NoneAuth
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<String> login(@Valid @RequestBody UserLoginReqVO reqVO, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return result(bindingResult);
        }
        return userService.login(reqVO);
    }

    @GetMapping("/logout")
    @ApiOperation("登出")
    public Result logout(HttpServletRequest request) {
        return userService.logout(request);
    }

    @NoneAuth
    @PostMapping("/add")
    @ApiOperation("新增用户")
    public Result add(@Valid @RequestBody AddUserReqVO reqVO, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return result(bindingResult);
        }
        return userService.add(reqVO);
    }

    @NoneAuth
    @GetMapping("/checkMobile")
    @ApiOperation("检查手机号是否存在")
    public Result checkMobile(@RequestParam("mobile") String mobile) {
        return userService.checkMobile(mobile);
    }

    @NoneAuth
    @PostMapping("/resetPassword")
    @ApiOperation("重置密码")
    public Result resetPassword(@Valid @RequestBody ResetPasswordReqVO reqVO, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return result(bindingResult);
        }
        return userService.resetPassword(reqVO);
    }

    @PostMapping("/update")
    @ApiOperation("修改个人信息")
    public Result update(@Valid @RequestBody UpdateUserReqVO reqVO, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return result(bindingResult);
        }
        return userService.update(reqVO);
    }

    @GetMapping("/getSelfInfo")
    @ApiOperation("获取个人信息")
    public Result<SelfInfoVO> getSelfInfo() {
        return userService.getSelfInfo();
    }
}
